@extends('base')

@section('content')
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form layui-form-pane form-search" action="javascript:;">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">账号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="account" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">菜单名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="node" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">IP</label>
                    <div class="layui-input-inline">
                        <input type="text" name="ip" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">操作时间</label>
                    <div class="layui-input-inline">
                        <input type="text" name="createtime" id="createtime" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="record-query"><i class="layui-icon layui-icon-search"></i>查询</button>
                    <button type="reset" class="pear-btn pear-btn-md"><i class="layui-icon layui-icon-refresh"></i>重置</button>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="layui-card">
    <div class="layui-card-body">
        <table id="record-table" lay-filter="record-table"></table>
    </div>
</div>

<script type="text/html" id="record-toolbar">
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove"><i class="layui-icon layui-icon-delete"></i>删除</button>
    <button class="pear-btn pear-btn-warming pear-btn-md" lay-event="allRemove"><i class="layui-icon layui-icon-close-fill"></i>清空</button>
</script>

<script type="text/html" id="record-bar">
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
</script>

<script type="text/html" id="record-createTime">
   @{{ layui.util.toDateString(d.create_time, 'yyyy-MM-dd HH:mm:ss') }}
</script>
@endsection

@section('js')
<script>
    layui.use(['table', 'form', 'jquery','common', 'laydate'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let laydate = layui.laydate;

        laydate.render({
            elem: '#createtime'
            ,type: 'datetime'
            ,range: true
            ,format: 'yyyy/MM/dd/ HH:mm:ss'
        });

        let cols = [
            [
                {type: 'checkbox'},
                {title: 'ID', field: 'id', align: 'center', width: 80},
                {title: '账号', field: 'log_account', align: 'center', width: 100},
                {title: '菜单名', field: 'log_menu', align: 'center'},
                {title: '请求方式', field: 'log_method', align: 'center',},
                {title: '传递参数', field: 'log_params', align: 'center',},
                {title: '行为描述', field: 'log_desc', align: 'center',},
                {title: 'IP地址', field: 'log_ip', align: 'center',},
                {title: '操作时间', field: 'create_time', align: 'center', templet: '#record-createTime'},
                {title: '操作', toolbar: '#record-bar', align: 'center', width: 130}
            ]
        ]

        table.render({
            elem: '#record-table',
            url: '/record/list',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#record-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('tool(record-table)', function(obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            }
        });

        table.on('toolbar(record-table)', function(obj) {
            if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }else if (obj.event === 'allRemove') {
                window.allRemove(obj);
            }
        });

        form.on('submit(record-query)', function(data) {
            table.reload('record-table', {
                where: data.field
            })
            return false;
        });

        window.remove = function(obj) {
            layer.confirm('确定要删除该条记录吗？', {icon: 3, title: '提示'}, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/record/delete/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    success: function(result) {
                        layer.close(loading);
                        if (result.status) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function() {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }

        window.batchRemove = function(obj) {
            let checkIds = common.checkField(obj,'id');
            if (checkIds === "") {
                layer.msg("未选中数据", {icon: 3, time: 1000});
                return false;
            }
            layer.confirm('确定要删除这些记录吗？', {icon: 3, title: '提示'}, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/record/remove/?ids=" + checkIds,
                    dataType: 'json',
                    type: 'delete',
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    success: function(result) {
                        layer.close(loading);
                        if (result.status) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function() {
                                table.reload('record-table');
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }

        window.allRemove = function(obj) {
            layer.confirm('确定要清空日志记录吗？', {icon: 3, title: '提示'}, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/record/clear/",
                    dataType: 'json',
                    type: 'delete',
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    success: function(result) {
                        layer.close(loading);
                        if (result.status) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function() {
                                table.reload('record-table');
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }

        window.refresh = function(param) {
            table.reload('record-table');
        }
    })
</script>
@endsection


